package edu.cebanc.spring.biblioteca.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import edu.cebanc.spring.biblioteca.domain.Publisher;

@Repository("publishersDAO")
@Transactional(propagation=Propagation.SUPPORTS, readOnly = true)
public class PublishersDAOimpl implements PublishersDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void savePublisher(Publisher pub) {
		sessionFactory.getCurrentSession().saveOrUpdate(pub);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Publisher> listPublishers() {
		return (List<Publisher>) sessionFactory.getCurrentSession().createCriteria(Publisher.class).list();
	}

	@Override
	public Publisher getPublisher(int id) {
		return (Publisher) sessionFactory.getCurrentSession().load(Publisher.class, id);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly = false)
	public void delPublisher(Publisher pub) {
		sessionFactory.getCurrentSession().delete(pub);
	}
}
